//
//  CLIndexedTableView.m
//  Charles
//
//  Created by Charles on 2019/3/13.
//  Copyright © 2019 charles. All rights reserved.
//

#import "CLIndexedTableView.h"
#import "ViewReusePool.h"

@implementation CLImageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    [self.contentView addSubview:self.myImageView];
    
    [self.myImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.contentView).offset(20);
        make.width.equalTo(160);
        make.bottom.equalTo(self.contentView).offset(-20);
    }];
}

- (UIImageView *)myImageView {
    if (!_myImageView) {
        _myImageView = [[UIImageView alloc]init];
        _myImageView.backgroundColor = [UIColor lightGrayColor];
        _myImageView.clipsToBounds = YES;
        _myImageView.layer.cornerRadius = 80;
        _myImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _myImageView;
}

@end

@interface CLIndexedTableView (){
    ViewReusePool *_reusePool;
    UIView *_containerView;
}

@end

@implementation CLIndexedTableView

- (void)reloadData {
    [super reloadData];
    if (_containerView == nil) {
        _containerView = [[UIView alloc]initWithFrame:CGRectZero];
        _containerView.backgroundColor = [UIColor whiteColor];
        // 避免索引条随着table滚动
        [self.superview insertSubview:_containerView aboveSubview:self];
    }
    
    if (_reusePool == nil) {
        _reusePool = [[ViewReusePool alloc]init];
    }
    
    // 标记所有视图为可重用状态
    [_reusePool reset];
    
    
    // reload 字母索引条
    [self reloadIndexedBar];
}

- (void)reloadIndexedBar {
    // 获取字母索引条显示内容
    NSArray *arrayTitles = nil;
    if ([self.indexedDataSource respondsToSelector:@selector(indexTitlesForIndexTableView:)]) {
        arrayTitles = [self.indexedDataSource indexTitlesForIndexTableView:self];
    }
    
    // 判断字母索引条是否为空
    if (!arrayTitles || arrayTitles.count < 0) {
        _containerView.hidden = YES;
        return;
    }
    
    NSInteger count = arrayTitles.count;
    CGFloat buttonWidth = 60;
    CGFloat buttonHeight = self.frame.size.height / count;
    
    for (int i = 0; i<arrayTitles.count; i++) {
        NSString *title = [arrayTitles objectAtIndex:i];
        
        // 从重用池中取出一个Button
        UIButton *button = (UIButton *)[_reusePool dequeueReuseableView];
        if (nil == button) {
            button = [[UIButton alloc]initWithFrame:CGRectZero];
            button.backgroundColor = [UIColor whiteColor];
            // 注册button到重用池当中
            [_reusePool addUsingView:button];
            NSLog(@"新创建一个button");
        }else {
            NSLog(@"button 重用了");
        }
        // 添加button到父视图控件
        [_containerView addSubview:button];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.frame = CGRectMake(0, i*buttonHeight, buttonWidth, buttonHeight);
    }
    _containerView.hidden = NO;
    _containerView.frame = CGRectMake(self.frame.origin.x + self.frame.size.width - buttonWidth, self.frame.origin.y, buttonWidth, self.frame.size.height);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
